﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controller;
using ManageStore.IViews;
using BusinessModel.Reports;

namespace ManageStore.Forms
{
    public partial class FrmReport : Form
    {
        public FrmReport()
        {
            InitializeComponent();
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("InStockRpt", "Báo cáo tồn kho"),
                new KeyValuePair<string, string>("InStockBuyRpt", "Báo cáo nhập"),
                new KeyValuePair<string, string>("InStockSaleRpt", "Báo cáo xuất")
            };
            this.listBox1.DisplayMember = "Value";
            this.listBox1.ValueMember = "Key";
            this.listBox1.DataSource = lstSource;

            PageSize = 20;
            PageSize = Convert.ToInt32(global::ManageStore.Properties.Settings.Default.PageSize);
        }

        #region Properties
        // ReportCtr.
        private ReportCtr _ReportCtr;
        public ReportCtr ReportCtr
        {
            get
            {
                if (_ReportCtr == null)
                {
                    _ReportCtr = new ReportCtr();
                }
                return _ReportCtr;
            }
            set { _ReportCtr = value; }
        }

        public int PageSize { get; set; }
        #endregion Properties

        private List<ProductBusinessRpt> Mtd_CreateDataSource(int pSize)
        {
            List<ProductBusinessRpt> result = new List<ProductBusinessRpt>();
            for (int i = 0; i < pSize; i++)
            {
                // OrderBy(TypeName).
                result.Add(new ProductBusinessRpt() { TypeName = "zzzzz" });
            }
            return result;
        }

        public object Mtd_CreateReportViewerForm()
        {
            string sReportTitle = listBox1.Text;
            string sReportSource = listBox1.SelectedValue.ToString();
            List<ProductBusinessRpt> lstDataSource = null;
            bool bIsConsign = false;
            FrmReportViewer oFrmReportViewer = new FrmReportViewer();
            List<ProductBusinessRpt> lstProductBusinessRpt;
            switch (sReportSource)
            {
                case "InStockRpt":
                    bIsConsign = true;
                    oFrmReportViewer.ProductCount = ReportCtr.ProductCount().ToResult<int>();
                    lstProductBusinessRpt = Mtd_CreateDataSource(oFrmReportViewer.ProductCount - PageSize);
                    oFrmReportViewer.ProductIndex = 0;
                    lstDataSource = ReportCtr.ProductInStock(oFrmReportViewer.ProductIndex, PageSize).ToResult<List<ProductBusinessRpt>>();
                    lstDataSource.AddRange(lstProductBusinessRpt);
                    oFrmReportViewer.EvtPageNavigation += (sender, evg) =>
                        {
                            int iIndex = (evg.NewPage - 1) * PageSize;
                            while (iIndex > oFrmReportViewer.ProductIndex)
                            {
                                oFrmReportViewer.ProductIndex += PageSize;
                                lstDataSource = oFrmReportViewer.LstDataSource as List<ProductBusinessRpt>;
                                lstProductBusinessRpt = ReportCtr.ProductInStock(oFrmReportViewer.ProductIndex, PageSize).ToResult<List<ProductBusinessRpt>>();
                                if (lstProductBusinessRpt != null)
                                {
                                    for (int i = 0; i < PageSize; i++)
                                    {
                                        if (lstDataSource.Count <= oFrmReportViewer.ProductIndex + i)
                                            break;
                                        // Remove Empty Data.
                                        lstDataSource.RemoveAt(oFrmReportViewer.ProductIndex + i);
                                        // Insert Continue Data.
                                        lstDataSource.Insert(oFrmReportViewer.ProductIndex + i, lstProductBusinessRpt[i]);
                                    }
                                    oFrmReportViewer.LstDataSource = lstDataSource;
                                    //oFrmReportViewer.RefreshReport();
                                }
                            }
                        };
                    break;
                case "InStockBuyRpt":
                    oFrmReportViewer.ProductCount = ReportCtr.ProductCount().ToResult<int>();
                    lstDataSource = ReportCtr.ProductInStockBuy().ToResult<List<ProductBusinessRpt>>();
                    break;
                case "InStockSaleRpt":
                    oFrmReportViewer.ProductCount = ReportCtr.ProductSaleCount().ToResult<int>();
                    lstDataSource = ReportCtr.ProductInStockSale().ToResult<List<ProductBusinessRpt>>();
                    break;
            }
            oFrmReportViewer.Mtd_LoadReport("ManageStore.Reports." + sReportSource + ".rdlc", sReportSource, sReportTitle, lstDataSource, bIsConsign);
            return oFrmReportViewer;
        }

        private void btnPreView_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedValue != null)
            {
                string sReportName = listBox1.SelectedValue.ToString();
                IMainView oFrmMain = this.MdiParent as IMainView;
                oFrmMain.ShowForm(sReportName, Mtd_CreateReportViewerForm);
            }
        }
    }
}
